import { find, Prefab, Sprite, SpriteFrame } from 'cc';
import { view } from 'cc';
import { instantiate } from 'cc';
import { tween } from 'cc';
import { UITransform } from 'cc';
import { _decorator, Component, Label, Node } from 'cc';
import { App } from '../../../App';
import AbstractComponent from '../../../base/AbstractComponent';
import { EventConst } from '../../../common/Common';
import { PD } from '../../../player/PlayerData';
import { showLuckyBallRule, showTipBox } from '../../../tools/PopuUp';
import UtilTool from '../../../tools/UtilTool';
import { BLACK_BALL } from '../GameManage';
import { GameModel } from '../model/GameModel';
import { GameLayer3 } from './GameLayer3';
const { ccclass, property } = _decorator;

// 一开始展示的目标球
@ccclass('GameMulitMyBall')
export class GameMulitMyBall extends Component {
    @property(Node)
    contentNode : Node = null

    @property(Prefab)
    ball_item : Prefab = null

    start() {
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())
        UtilTool.nodeActive(this.contentNode, 5)
    }

    setData(cb) {
        setTimeout(() => {
            cb && cb()
            if (this.node) {
                this.node.destroy()
            }
        }, 2000);

        let targetBall = App.Model.gameManage.gameStartData.targetBall
        let ballArr = null
        for (const key in targetBall) {
            const value = targetBall[key]
            if (key == PD.user.addressUrl) {
                ballArr = value
                break
            }
        }

        if (ballArr==null) {
            return
        }

        ballArr = ballArr.sort((number1, number2)=>{ // 增序
            return number1 - number2
        })

        // let spDict = App.Scene.getRes("images/ball2D_2")
        let bigDict = App.Scene.getRes("images/ball2D")
        let slotNode = find("slot",this.contentNode)
        for (let index = 0; index < ballArr.length; index++) {
            let num = ballArr[index];
            let node = instantiate(this.ball_item)

            // if(num < BLACK_BALL) {
            //     node.getComponent(Sprite).spriteFrame = spDict["ball2d_" + item]
            // } else {
                node.getComponent(Sprite).spriteFrame = bigDict["ball2d_" + num]
            // }

            node.parent = slotNode
            node.getComponent(UITransform).setContentSize(110,110)
            let posx = UtilTool.getSortPosX(ballArr.length, 110, index)
            node.setPosition(posx, 0)
        }
    }

    /** 
     * 确定
      */
    clickSure() {
        App.Audio.playOneShot("click")
        this.node.destroy()
    }

    onDestroy() {
        
    }

}

